/**
 * @file search.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_SEARCH != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
#if 0
static lv_res_t lv_search_obj_signal(lv_obj_t * obj, lv_signal_t sign, void * param);
#endif
static uint8_t search_anim_playtime = 0;
static void search_right_slide_callback(lv_obj_t * obj);
static void search_btn_action(void);
static void search_cleanup(void);
static void search_gif_anim_run(lv_obj_t * obj);
static void search_anim_play_tick_timeout(lv_anim_t * anim);
static void search_destroy(void);
static void search_prepare_destory(lv_obj_t * activity_obj);
static lv_obj_t * search_create(lv_obj_t * activity_obj);
static void search_create_process_task(lv_task_t * task);
static void search_destroy_process_task(lv_task_t * task);

/**********************
 *  STATIC VARIABLES
**********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void search_create_event_cb(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED == event) {
        lv_watch_png_cache_all_free();

        lv_obj_t * obj = search_create(NULL);
        LV_ASSERT_MEM(obj);
        if(obj == NULL) return;
    }
}

void search_create_process(void)
{
    if(0 == Hal_Pm_Get_State()) {
        Hal_Pm_WakeUp();
    } else {
        watch_wakeup_lcd(false);
    }
    lv_task_t * search_task_once = lv_task_create(search_create_process_task, 100, LV_TASK_PRIO_HIGH, NULL);
    lv_task_once(search_task_once);
    lv_task_ready(search_task_once);
    printf("%s: search_task_once has been created\n",__FUNCTION__);
}

void search_destroy_process(void)
{
    if(0 == Hal_Pm_Get_State()) {
        Hal_Pm_WakeUp();
    } else {
        watch_wakeup_lcd(false);
    }
    lv_task_t * search_task_once = lv_task_create(search_destroy_process_task, 100, LV_TASK_PRIO_HIGH, NULL);
    lv_task_once(search_task_once);
    lv_task_ready(search_task_once);
    printf("%s: search_task_once has been created\n",__FUNCTION__);
}
/**********************
 *   STATIC FUNCTIONS
 **********************/
static void search_create_process_task(lv_task_t * task)
{
    printf("%s: search_create\n",__FUNCTION__);
    search_create_event_cb(NULL, LV_EVENT_CLICKED);
}

static void search_destroy_process_task(lv_task_t * task)
{
    if(lv_watch_get_activity_obj(ACT_ID_SEARCH))
    {
        printf("%s: search_destroy\n",__FUNCTION__);
        search_destroy();
    }
}

static void search_audio_ctrl_callback(AUDIO_CTRL_PRIORITY priority)
{
    printf("%s,priority is %d\n", __FUNCTION__, priority);
    if(AUDIO_CTRL_PRIORITY_SEARCH == priority) {
            if (AUDIO_CTRL_PRIORITY_SEARCH == Hal_Audio_Manage_Start_Req(AUDIO_CTRL_PRIORITY_SEARCH, search_audio_ctrl_callback))
            {
                Hal_NFFS_File_Play_Repeat(AUDIO_SEARCH, HAL_AUDIO_SPK_LEVEL_10);
                watch_set_sleep_lcd_only(true);
            }
    } else if(AUDIO_CTRL_PRIORITY_SEARCH > priority) {
        Hal_Tone_Play_End();
        watch_set_sleep_lcd_only(false);
    }
}

static lv_obj_t * search_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_SEARCH;
        activity_ext.create = search_create;
        activity_ext.prepare_destory = search_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }
    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_search_ext_t * ext =
        lv_obj_allocate_ext_attr(activity_obj, sizeof(lv_search_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;
    ext->searchobj = obj;
#if 0
    lv_obj_set_signal_func(obj, lv_search_obj_signal);
#endif
    lv_watch_obj_set_anim_mode(obj, LV_WATCH_LEFT_OR_RIGHT_SLIDE_CALLBACK);
    lv_watch_obj_set_right_slide_cb(obj, search_right_slide_callback);

    ext->gif_info = gif_open(obj, ANIM_SEARCH, 0);
    if(!ext->gif_info) {
        return obj;
    }
    search_anim_playtime = 0;
    search_gif_anim_run(obj);
    ext->search_music_status = true;
    //play music
    if (AUDIO_CTRL_PRIORITY_SEARCH == Hal_Audio_Manage_Start_Req(AUDIO_CTRL_PRIORITY_SEARCH, search_audio_ctrl_callback))
    {
        Hal_NFFS_File_Play_Repeat(AUDIO_SEARCH, HAL_AUDIO_SPK_LEVEL_10);
        watch_set_sleep_lcd_only(true);
    }

    return obj;
}

static void search_right_slide_callback(lv_obj_t * obj)
{
    search_destroy();
}
#if 0
static lv_res_t lv_search_obj_signal(lv_obj_t * obj, lv_signal_t sign, void * param)
{
    if(sign == LV_SIGNAL_RELEASED) {
        lv_search_ext_t * ext = lv_obj_get_ext_attr(lv_watch_get_activity_obj(ACT_ID_SEARCH));
        if(true == ext->search_music_status) {
            //music is on
            //mute the music
            search_btn_action();
        }
    }
    lv_watch_obj_signal(obj, sign, param);
    return LV_RES_OK;
}
#endif
static void search_btn_action(void)
{
    //mute the music
    if (AUDIO_CTRL_PRIORITY_SEARCH == Hal_Audio_Manage_Start_Req(AUDIO_CTRL_PRIORITY_SEARCH, search_audio_ctrl_callback))
    {
        Hal_Tone_Play_End();
        watch_set_sleep_lcd_only(false);
    }
    Hal_Audio_Manage_End_Ind(AUDIO_CTRL_PRIORITY_SEARCH);
    lv_search_ext_t * ext = lv_obj_get_ext_attr(lv_watch_get_activity_obj(ACT_ID_SEARCH));
    ext->search_music_status = false;
}
static void search_cleanup(void)
{
    lv_search_ext_t * ext = lv_obj_get_ext_attr(lv_watch_get_activity_obj(ACT_ID_SEARCH));
    if(true == ext->search_music_status) {
        //music is on
        //mute the music
        search_btn_action();
    }
    gif_close(ext->gif_info);
    ext->gif_info = NULL;
    search_anim_playtime = 0;
    ext->search_music_status = false;
    ext->searchobj = NULL;
#if USE_LV_BLE_DEMO != 0
    ble_fmp_service_stop();
#endif
}
static void search_destroy(void)
{
    lv_watch_png_cache_all_free();
    search_cleanup();
    lv_obj_del_async(lv_watch_get_activity_obj(ACT_ID_SEARCH));
}
static void search_gif_anim_run(lv_obj_t * obj)
{
    lv_anim_t a = {};
    lv_anim_init(&a);
    lv_anim_set_var(&a, obj);
    lv_anim_set_start_cb(&a, search_anim_play_tick_timeout);
    lv_anim_set_time(&a, 1);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_repeat_delay(&a, 1000);

    lv_anim_start(&a);
}
static void search_anim_play_tick_timeout(lv_anim_t * anim)
{    if(SEARCH_ANIM_PLAY_TIME == search_anim_playtime) {
        search_destroy();
    }
    search_anim_playtime++;
}
static void search_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
    search_cleanup();
}

#endif /*USE_LV_WATCH_PHONE*/
